package com.huiruan.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huiruan.entity.Category;
import com.huiruan.service.CategoryService;
import com.huiruan.utils.Result;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * @author CTL
 * @create 2025-05-13 21:21
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/category")
public class CategoryController {
    private  final CategoryService categoryService;
    /**
     * 分页查询
     * @param page 分页对象
     * @param category
     * @return
     */
    //@ApiOperation(value = "分页查询", notes = "分页查询")
    @Operation(summary = "分页查询", description = "返回所有商品分类数据")
    @GetMapping("/page" )
    //@PreAuthorize("@pms.hasPermission('midms_tstock_get')" )
    public Result getCategoryPage(Page page, Category category) {
        return Result.success(categoryService.page(page, Wrappers.query(category)));
    }


    /**
     * 通过id查询
     * @param code id
     * @return R
     */
    @Operation(summary = "通过id查询", description = "返回所有商品分类数据")
    @GetMapping("/{code}" )
    public Result getById(@PathVariable("code" ) String code) {
        return Result.success(categoryService.getById(code));
    }

    /**
     * 新增
     * @param category
     * @return R
     */
    @Operation(summary = "新增", description = "新增")
    @PostMapping
    public Result save(@RequestBody Category category) {
        return Result.success(categoryService.save(category));
    }

    /**
     * 修改
     * @param category
     * @return R
     */
    @Operation(summary = "修改", description = "修改")
    @PutMapping
    public Result updateById(@RequestBody Category category) {
        return Result.success(categoryService.updateById(category));
    }

    /**
     * 通过id删除
     * @param code id
     * @return R
     */
    //@ApiOperation(value = "通过id删除", notes = "通过id删除")
    @Operation(summary = "通过id删除", description = "通过id删除")
    @DeleteMapping("/{id}" )
    public Result removeById(@PathVariable Long id) {
        return Result.success(categoryService.removeById(id));
    }

}
